package marksys.free.com.marksys.repo;

import android.arch.lifecycle.LiveData;
import android.arch.lifecycle.MediatorLiveData;
import android.arch.lifecycle.MutableLiveData;
import java.util.List;
import javax.inject.Inject;
import marksys.free.com.marksys.nettool.ApiResult;
import marksys.free.com.marksys.nettool.MyApi;
import marksys.free.com.marksys.nettool.common.Resource;
import marksys.free.com.marksys.nettool.vo.History;
import marksys.free.com.marksys.nettool.vo.ScoreRecords;
import marksys.free.com.marksys.utils.UnboxUtils;

public class HistoryRepo {
    private final MyApi.ApiService apiService;

    @Inject
    HistoryRepo(MyApi.ApiService apiService) {
        this.apiService = apiService;
    }

    private static final int PAGE_SIZE = 20;
    private MediatorLiveData<Resource<List<ScoreRecords>>> mediatorLiveData = new MediatorLiveData<>();
    private MediatorLiveData<Resource<Boolean>> loadMoreStatus = new MediatorLiveData<>();
    private MutableLiveData<Boolean> hasMoreData = new MutableLiveData<>();

    private List<ScoreRecords> scoreRecords;
    private int page;
    private int type;

    public LiveData<Resource<List<ScoreRecords>>> history(int type) {
        this.type = type;//洁律勤
        this.page = 1;
        this.scoreRecords = null;

        mediatorLiveData.setValue(Resource.loading(null));
        LiveData<ApiResult<History>> apiRequest = apiService.history(type, page, PAGE_SIZE);
        mediatorLiveData.addSource(apiRequest, historyListApiResult -> {
            assert historyListApiResult != null;
            if (historyListApiResult.isSuccessful()) {
                if (null != scoreRecords) {
                    historyListApiResult.data.rows.addAll(0, scoreRecords);
                }
                scoreRecords = historyListApiResult.data.rows;
                mediatorLiveData.setValue(Resource.success(scoreRecords));
                hasMoreData.postValue(historyListApiResult.data.page < historyListApiResult.data.pageCount);
            } else {
                mediatorLiveData.setValue(Resource.error(historyListApiResult.code, historyListApiResult.errorMsg(), null));
            }
            mediatorLiveData.removeSource(apiRequest);
        });
        return mediatorLiveData;
    }


    public LiveData<Boolean> hasMoreData() {
        return hasMoreData;
    }

    /**
     * @return boolean hasMoreData
     */
    public LiveData<Resource<Boolean>> loadMore() {
        if (!UnboxUtils.safeUnBox(hasMoreData.getValue())) {
            return loadMoreStatus;
        }
        page++;
        LiveData<ApiResult<History>> apiRequest = apiService.history(type, page, PAGE_SIZE);
        loadMoreStatus.addSource(apiRequest, historyListApiResult -> {
            assert historyListApiResult != null;
            if (historyListApiResult.isSuccessful()) {
                if (null != scoreRecords) {
                    historyListApiResult.data.rows.addAll(0, scoreRecords);
                }
                scoreRecords = historyListApiResult.data.rows;
                mediatorLiveData.setValue(Resource.success(scoreRecords));
                loadMoreStatus.setValue(Resource.success(historyListApiResult.data.page < historyListApiResult.data.pageCount));
                hasMoreData.postValue(historyListApiResult.data.page < historyListApiResult.data.pageCount);
            } else {
                loadMoreStatus.setValue(Resource.error(historyListApiResult.code, historyListApiResult.errorMsg(), true));
            }
            mediatorLiveData.removeSource(apiRequest);
        });
        loadMoreStatus.setValue(Resource.loading(null));
        return loadMoreStatus;
    }

}

